using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Common;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.ImageAlbum;
using NTTHAO.Imaging;
using System.IO;

public partial class admin_webparts_Plug_ins_Images_ImageInput : System.Web.UI.UserControl
{
    public ImageAlbum _Album = null;
    public AlbumDetail _Detail = null;
    public string _ErrorMessage = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);

        if (!IsPostBack)
        {
            try
            {
                int albumID = Int32.Parse(Request.QueryString["albumid"]);
                ImageAlbum album = new ImageAlbum(albumID);
                if (album.ID > 0)
                {
                    _Album = album;

                    if ("imageedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
                    {
                        AlbumDetail detail = new AlbumDetail(Int32.Parse(Request.QueryString["imageid"]));
                        if (detail.ID > 0 && detail.AlbumID == album.ID)
                        {
                            this._Detail = detail;
                            this.txtImageName.Text = detail.ImageName;
                            this.txtURL.Text = detail.LinkURL.Replace("http://", "");
                            this.txtDescription.Text = detail.Description;
                            this.chkActive.Checked = detail.Active;
                        }
                        else
                        {
                            Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewalbum&albumid=" + _Album.ID.ToString());
                        }
                    }
                }
                else
                {
                    Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"]);
                }
            }
            catch (Exception)
            {

            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ImageAlbum album = new ImageAlbum(Int32.Parse(Request.QueryString["albumid"].Trim()));
            AlbumDetail detail = null;
            if ("imageedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                detail = new AlbumDetail(Int32.Parse(Request.QueryString["imageid"].Trim()));
            }
            else
            {
                detail = new AlbumDetail();
            }
            detail.ImageName = txtImageName.Text;
            string url = txtURL.Text.Trim();
            detail.LinkURL = (url.Length > 0 && !url.Equals("#")) ? ("http://" + url.Replace("http://", "")) : "#";
            detail.Active = chkActive.Checked;
            detail.Description = txtDescription.Text;

            string uploadedFileName = String.Empty;
            string filePath = "../" + Constant.UPLOAD_FILE_PATH + "album/";
            if (this.fuImage.PostedFile != null)
            {
                if (this.fuImage.PostedFile.FileName.Contains("."))
                {
                    uploadedFileName = DateTime.Now.Ticks.ToString() + this.fuImage.PostedFile.FileName.Substring(this.fuImage.PostedFile.FileName.LastIndexOf("."));

                    this.fuImage.PostedFile.SaveAs(Server.MapPath(filePath + uploadedFileName));
                }
            }

            if ("imageedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                if (uploadedFileName != string.Empty)
                {
                    if (System.IO.File.Exists(Server.MapPath(filePath + album.ID.ToString() + "/" + detail.ImageFileName)))
                    {
                        System.IO.File.Delete(Server.MapPath(filePath + album.ID.ToString() + "/" + detail.ImageFileName));
                    }
                }
                detail.Update();
            }
            else
            {
                detail.AlbumID = album.ID;
                detail.ImageFileName = "";
                detail.Insert();
            }

            if (uploadedFileName != string.Empty)
            {
                detail.ImageFileName = "Image_" + detail.ID.ToString() + "_" + DateTime.Now.Ticks.ToString() + uploadedFileName.Substring(uploadedFileName.LastIndexOf("."));
                string serverFilePath = Server.MapPath(filePath + album.ID.ToString() + "/" + detail.ImageFileName);
                if (System.IO.File.Exists(serverFilePath))
                {
                    System.IO.File.Delete(serverFilePath);
                }
                System.IO.File.Move(Server.MapPath(filePath + uploadedFileName), serverFilePath);

                // resize
                {
                    int width = album.DefaultWidth, height = album.DefaultHeight;
                    if (width > 0 && height > 0)
                    {
                        serverFilePath = ImageUtilities.ResizeImageToJpeg(serverFilePath, width, height, ResizeOptions.MaximumSizeFix, System.Drawing.Color.White, 95);
                        detail.ImageFileName = Path.GetFileName(serverFilePath);
                    }
                }

                //layer mask
                if (Constant.ADD_LAYER_MASK)
                {
                    float left = 0, top = 0, width = 0, height = 0, opacity = 1;
                    float.TryParse(ConfigValues.GetConfigValue("MASK_TOP_PERCENT").Trim(), out top);
                    float.TryParse(ConfigValues.GetConfigValue("MASK_LEFT_PERCENT").Trim(), out left);
                    float.TryParse(ConfigValues.GetConfigValue("MASK_WIDTH_PERCENT").Trim(), out width);
                    float.TryParse(ConfigValues.GetConfigValue("MASK_HEIGHT_PERCENT").Trim(), out height);
                    float.TryParse(ConfigValues.GetConfigValue("MASK_OPACITY").Trim(), out opacity);

                    ImageUtilities.DrawLayerMask(serverFilePath, Server.MapPath("../" + Constant.MASK_IMAGE_FILE), top, left, width, height, ResizeOptions.MaximumSizeFix, opacity, 95);
                }
                // end

                detail.Update();
            }

            Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewalbum&albumid=" + album.ID.ToString());
        }
        catch (Exception ex)
        {
            this._ErrorMessage = ex.Message;
        }
    }
}
